package com.example.hzq.zqb.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.activity.ShopdetailActivity;
import com.example.hzq.zqb.bean.CampusShopBean;
import com.example.hzq.zqb.model.Model;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.zqb.fragment.Fragment1.imageurl;

/**
 * Created by hzq on 2017/7/7.
 */

public class CampusshopAdapter extends RecyclerView.Adapter{
    public   static int goodid;
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<CampusShopBean> list;//保存要显示的数据
    public CampusshopAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    public void setList(List<CampusShopBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView shoppic;
        private TextView goodname,bossname,createtime,price,count,hint;
        public ViewHolder(View itemView) {
            super(itemView);
            shoppic=(ImageView)itemView.findViewById(R.id.campusshoppic);
           goodname=(TextView)itemView.findViewById(R.id.goodsname);
           bossname=(TextView)itemView.findViewById(R.id.bossname);
           createtime=(TextView)itemView.findViewById(R.id.goodscreattime);
           price=(TextView)itemView.findViewById(R.id.goodsprice);
           count=(TextView)itemView.findViewById(R.id.goodscount);
           hint=(TextView)itemView.findViewById(R.id.goodshint);
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.campusitem, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final CampusShopBean campusShopBean = list.get(position);
        if (campusShopBean == null)
            return;
        final ViewHolder viewholder = (ViewHolder) holder;
        Picasso.with(context).load(imageurl+campusShopBean.getHead_img()).placeholder(R.drawable.ic_default_adimage).into(viewholder.shoppic);
        viewholder.goodname.setText(campusShopBean.getGoodsname());
        viewholder.bossname.setText("卖家:"+campusShopBean.getU_id());
        viewholder.createtime.setText("上架时间:"+campusShopBean.getCreatetime());
        viewholder.price.setText("￥"+campusShopBean.getPrice());
        viewholder.count.setText("库存:"+campusShopBean.getCount());
        viewholder.hint.setText("浏览量:"+campusShopBean.getHint());
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodid=Integer.parseInt(campusShopBean.getId());
                Model model=new Model();
                Call<String>call=model.changeHint(goodid);
                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        if (response.body().toString().equals("success")){
                            list.get(position).setHint((Integer.parseInt(list.get(position).getHint())+1)+"");
                            setList(list);
                            Intent intent=new Intent();
                            intent.setClass(context, ShopdetailActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("campusshop",campusShopBean);
                            intent.putExtras(bundle);
                            context.startActivity(intent);
                        }
                        else
                            Toast.makeText(context,"修改浏览量失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        Toast.makeText(context,"404.campusadapter",Toast.LENGTH_SHORT).show();
                    }
                });


            }
        });
    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        return list.size();
    }
}
